package com.tour.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tour.config.ResponseWrapper;
import com.tour.entity.Admin;
import com.tour.jwt.JWTUtil;
import com.tour.mapper.AdminMapper;
import com.tour.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tour.utils.Md5Util;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lel
 * @since 2021-01-21
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    AdminMapper adminMapper;


    public Admin findAdminByUsername(String username, String srole){
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("username",username).eq("srole",srole);
        Admin admin = adminMapper.selectOne(adminQueryWrapper);
        return admin;
    }
    public ResponseWrapper login(Admin admin){
        if (admin==null){
            return ResponseWrapper.markParamError();
        }else {
            Admin userInfo = null;
            Admin admins = findAdminByUsername(admin.getUsername(),admin.getSrole());
            if (admin == null) {
                return ResponseWrapper.markCustom(false, 400, "用户不存在", null);
            } else {
                String password = Md5Util.md5String(admin.getPassword()+admin.getUsername()+"tour");
                String realPassword = admins.getPassword();
                if (!realPassword.equals(password)) {
                    return ResponseWrapper.markCustom(false, 400, "密码错误", null);
                }
            }
            ResponseWrapper wrapper = ResponseWrapper.markSuccess(admins);
            wrapper.getData().put("data",admins);
            wrapper.getData().put("token",JWTUtil.createToken(admin.getUsername(),admin.getSrole()));
            return wrapper;
        }
    }

    public ResponseWrapper findAllAdmin(String content,Integer pageNum,Integer pageSize){
        Page<Admin> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.like("name",content);
        IPage<Admin> iPage =  adminMapper.selectPage(page,adminQueryWrapper);
        List list = iPage.getRecords();
        ResponseWrapper wrapper = ResponseWrapper.markSuccess(list);
        wrapper.getData().put("data",list);
        wrapper.getData().put("total",iPage.getTotal());
        wrapper.getData().put("pageSize",iPage.getSize());
        wrapper.getData().put("pageNum",iPage.getCurrent());
        return wrapper;
    }
    public ResponseWrapper addAdmin(Admin admin){
        if (admin == null){
            return ResponseWrapper.markError();
        }else {
            admin.setPassword(Md5Util.md5String(admin.getPassword()+admin.getUsername()+"tour"));
            QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
            adminQueryWrapper.eq("username",admin.getUsername()).eq("srole",admin.getSrole());
            int result = adminMapper.selectCount(adminQueryWrapper);
            if (result > 0){
                return ResponseWrapper.markCustom(false,500,"该账号已存在",null);
            }else {
                int i = adminMapper.insert(admin);
                if (i == 1){
                    return ResponseWrapper.markCustom(true,200,"注册成功",null);
                }else {
                    return ResponseWrapper.markError();
                }
            }
        }
    }
    public ResponseWrapper updateAdmin(Admin admin){
        if (admin == null){
            return ResponseWrapper.markError();
        }else {
            Admin admins = adminMapper.selectById(admin.getAdminId());
            String pasw = Md5Util.md5String(admin.getPassword()+admins.getUsername()+"tour");
            admin.setPassword(pasw);
            int result = adminMapper.updateById(admin);
            if (result == 1){
                return ResponseWrapper.markCustom(true,200,"修改成功",null);
            }else {
                return ResponseWrapper.markError();
            }
        }
    }
    public ResponseWrapper delAdmin(Integer adminId){
        if (adminId == null){
            return ResponseWrapper.markError();
        }else {
            int result = adminMapper.deleteById(adminId);
            if (result == 1){
                return ResponseWrapper.markCustom(true,200,"修改成功",null);
            }else {
                return ResponseWrapper.markError();
            }
        }
    }

}
